#!/usr/bin/python

import wx
import wx.grid
import lotto_db


class Picks(wx.Dialog):
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.DEFAULT_DIALOG_STYLE
        wx.Dialog.__init__(self, *args, **kwds)
        self.stats = wx.grid.Grid(self)
        self.button_1 = wx.Button(self, wx.ID_OK, "Done")
        self.__set_properties()
        self.__do_layout()
        self.dbconn = lotto_db.lotto_db()

    def __set_properties(self):
        self.SetTitle("Picks")
        self.stats.CreateGrid(6, 5)
        self.stats.SetColLabelValue(0, "Best \nSkip")
        self.stats.SetColLabelValue(1, "Most \nFrequent")
        self.stats.SetColLabelValue(2, "Most \nOverdue")
        self.stats.SetColLabelValue(3, "Last \n50 Draw")
        self.stats.SetColLabelValue(4, "Best \nGuess")

    def __do_layout(self):
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_1.Add(self.stats, 1, wx.EXPAND, 1)
        sizer_1.Add(self.button_1,
             0, wx.ALIGN_CENTER_HORIZONTAL | wx.ADJUST_MINSIZE, 0)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        self.Layout()

    def get_picks(self):
        skips = self.get_skips(self)
        frequent = self.get_frequencies(self)
        overdue = self.get_overdues(self)
        last50 = self.get_last50draws(self)
        print skips
        print frequent
        print overdue
        print last50

    def get_skips(self):
        skip_counts = []
        next_best_skip = []
        skip_due = []
        for i in range(1, 60):
            number_table = "whiteball_" + str(i)
            draw_row, draw_count = self.dbconn.get_last_row(number_table)
            best_skip, second_skip, third_skip = self.dbconn.get_highest_skips(
                i)
            best_skip_number, best_skip_count = self.dbconn.get_highest_skip(i)
            skip_counts.append((second_skip, third_skip, i))
            current_skip = self.dbconn.get_current_skip(i)
            if best_skip_number == current_skip:
                skip_due.append((best_skip_count, best_skip_number, i))
            if second_skip == current_skip:
                next_best_skip.append(i)
            if third_skip == current_skip:
                next_best_skip.append(i)
        skip_counts.sort()
        skip_counts.reverse()
        return skip_counts

    def get_frequencies(self):
        freq_counts = []
        for i in range(1, 60):
            number_table = "whiteball_" + str(i)
            draw_row, draw_count = self.dbconn.get_last_row(number_table)
            freq_counts.append((draw_count, i))
        freq_counts.sort()
        freq_counts.reverse()
        return freq_counts

    def get_overdues(self):
        overdue_dates = []
        for i in range(1, 60):
            number_table = "whiteball_" + str(i)
            draw_row, draw_count = self.dbconn.get_last_row(number_table)
            overdue_dates.append((draw_row[0], i))
        overdue_dates.sort()
        return overdue_dates

    def get_last50draws(self):
        return []

# end of class Picks

if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    dialog_1 = Picks(None, -1, "")
    app.SetTopWindow(dialog_1)
    dialog_1.Show()
    app.MainLoop()
